<template>
	<div>
		<use-table ref="tbl"></use-table>
		<div class="container padding_b_0">
			<div class="dflex_wrap">
				<div class="dflex_vertical_c margin_r_40 margin_b_20">
					<div class="search_name">分销人：</div>
					<el-input v-model="req.member_nickname" placeholder="请输入" class="search_input" @change="search"
						clearable></el-input>
				</div>
				<div class="dflex_vertical_c margin_r_40 margin_b_20" v-if="advancedSearch">
					<div class="search_name">分销时间：</div>
					<el-date-picker v-model="searchTime" type="daterange" align="right" range-separator="至"
						start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd"
						:picker-options="pickerTimeOptions" class="search_start_end_time" @change="search">
					</el-date-picker>
				</div>
				<el-button size="mini" :loading="searchBtnLoading" class="search-btn margin_b_20 margin_r_40"
					@click="search">搜索</el-button>
				<div class="search_advanced margin_b_20" @click="advancedSearch = !advancedSearch"
					v-if="!advancedSearch">高级筛选</div>
				<div class="search_common margin_b_20" @click="advancedSearch = !advancedSearch" v-if="advancedSearch">
					常用筛选</div>
			</div>
		</div>
		<div class="container last_container use-table">
			<el-table ref="etbl" :height="tblHeight" v-loading="tableLoading" :data="tableDatas" highlight-current-row
				@sort-change="sortable">
				<el-table-column label="分销人" align="center" width="190">
					<template slot-scope="scope">
						<div class="dflex_vertical_c">
							<el-image class="headimg_small" :src="
									(scope.row.fxs_member_id[0] && scope.row.fxs_member_id[0].member_headimg) ||
										'https://7463-tcb-rflrz31u8yan4rr9ad60d-268679-1308293149.tcb.qcloud.la/1659579992133_0.png'
								" :preview-src-list="scope.row.previewImgs" fit="cover"></el-image>
							<div class="margin_l_5 ellipsis_2 left_just">
								{{ scope.row.fxs_member_id[0] && scope.row.fxs_member_id[0].member_nickname || '--' }}
							</div>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="成员数量" align="center">
					<template slot-scope="scope">
						<div>{{ scope.row.fxs_member_cnt || 0 }}</div>
					</template>
				</el-table-column>
				<el-table-column label="成员订单" align="center">
					<template slot-scope="scope">
						<div>{{ scope.row.fxs_order_cnt || 0 }}</div>
					</template>
				</el-table-column>
				<el-table-column label="已提佣金" align="center">
					<template slot-scope="scope">
						<div class="price">{{ scope.row.fxs_profit_amount / 100 }}</div>
					</template>
				</el-table-column>
				<el-table-column label="待审提现" align="center">
					<template slot-scope="scope">
						<div class="price">{{ scope.row.fxs_unprofit_amount / 100 }}</div>
					</template>
				</el-table-column>
				<el-table-column label="待提金额" align="center">
					<template slot-scope="scope">
						<div class="price">{{ scope.row.fxs_cashout_amount / 100 }}</div>
					</template>
				</el-table-column>
				<el-table-column label="待结金额" align="center">
					<template slot-scope="scope">
						<div class="price">{{ scope.row.fxs_unsettled_amount / 100 }}</div>
					</template>
				</el-table-column>
				<el-table-column property="create_time" sortable="custom" label="分销时间" align="center" width="180">
				</el-table-column>
				<el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-tooltip :open-delay="350" content="详情" placement="top" :hide-after="1000" :enterable="false"
							effect="light">
							<el-button circle size="medium" icon="el-icon-more-outline" class="table-btn"
								@click="toDetail(scope.row._id)"></el-button>
						</el-tooltip>
					</template>
				</el-table-column>
				<div slot="empty">
					<el-empty description=" "></el-empty>
				</div>
			</el-table>
			<el-pagination background :current-page="req.page" :page-sizes="[10, 20, 30, 50, 100]" :page-size="req.rows"
				layout="total, sizes, prev, pager, next, jumper" :total="tableTotal" @size-change="sizeChange"
				@current-change="currentChange"></el-pagination>
		</div>
	</div>
</template>

<script>
	const __name = 'wmzmall-distributor, wmzmall-member';
	export default {
		data() {
			return {
				advancedSearch: false,
				searchTime: [],
				req: {
					page: 1,
					rows: 20,
					orderby: 'create_time desc',
					member_nickname: '',
					begin_time: '',
					end_time: ''
				},
				tblHeight: 0,
				tableLoading: false,
				tableDatas: [],
				tableTotal: 0,
				searchBtnLoading: false,
				pickerTimeOptions: {
					disabledDate(time) {
						return time.getTime() > Date.now();
					},
					shortcuts: [{
							text: '最近一周',
							onClick(picker) {
								const end = new Date();
								const start = new Date();
								start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
								picker.$emit('pick', [start, end]);
							}
						},
						{
							text: '最近一个月',
							onClick(picker) {
								const end = new Date();
								const start = new Date();
								start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
								picker.$emit('pick', [start, end]);
							}
						},
						{
							text: '最近三个月',
							onClick(picker) {
								const end = new Date();
								const start = new Date();
								start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
								picker.$emit('pick', [start, end]);
							}
						}
					]
				}
			};
		},
		methods: {
			async loadData(callback) {
				this.tableLoading = true;

				this.req.begin_time = '';
				this.req.end_time = '';

				if (this.searchTime && this.searchTime.length > 0) {
					this.req.begin_time = this.searchTime[0].getTime();
					this.req.end_time = this.searchTime[1].getTime() + (60 * 60 * 24 - 1) * 1000;
				}

				await this.$db[__name]
					.whereif(this.req.member_nickname, {
						'fxs_member_id.member_nickname': new RegExp(this.$api.filterRegexpKey(this.req
							.member_nickname), 'i')
					})
					.whereif(this.searchTime && this.searchTime.length > 0, {
						create_time: this.$db.$cmd.gte(this.req.begin_time).and(this.$db.$cmd.lte(this.req
							.end_time))
					})
					.totable(this.req)
					.then(res => {
						if (res.code == 200) {
							res.datas.rows.forEach((row, idx) => {
								row.previewImgs = [
									(row.fxs_member_id[0] && row.fxs_member_id[0].member_headimg && row
										.fxs_member_id[0].member_headimg.replace("/132", "/0")) ||
									'https://7463-tcb-rflrz31u8yan4rr9ad60d-268679-1308293149.tcb.qcloud.la/1659579992133_0.png'
								];
								row.create_time = new Date(row.create_time).format();
							});

							this.tableDatas = res.datas.rows;
							this.tableTotal = res.datas.total;

							if (typeof callback === 'function') {
								callback();
							}
						}
						this.searchBtnLoading = false;
						this.tableLoading = false;
					}).catch(() => {
						this.searchBtnLoading = false;
						this.tableLoading = false;
					})
			},
			search() {
				this.req.page = 1;
				this.$api.storagePaginationPage(this.req.page, this.$route.path);
				this.searchBtnLoading = true;

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});

				uni.setStorage({
					key: '__search' + this.$route.path,
					data: this.req
				});
			},
			sortable(column) {
				if (column.order) {
					column.order = column.order.replace('ending', '');
				}
				this.req.orderby = column.prop + ' ' + (column.order || 'desc');

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});
			},
			toDetail(id) {
				uni.navigateTo({
					url: `/pages/distribution/list/detail?id=${id}&tab=分销详情`,
					events: {
						refreshData: () => {
							this.loadData();
						}
					}
				});
			},
			sizeChange(size) {
				this.$api.storagePaginationRows(size, this.$route.path);
				this.req.page = 1;
				this.$api.storagePaginationPage(this.req.page, this.$route.path);
				this.req.rows = size;

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});
			},
			currentChange(current) {
				this.$api.storagePaginationPage(current, this.$route.path);
				this.req.page = current;

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});
			}
		},
		created() {
			let __search = uni.getStorageSync('__search' + this.$route.path);
			if (__search) {
				this.req = __search;
				if (this.req.begin_time && this.req.end_time) {
					this.searchTime.push(new Date(this.req.begin_time), new Date(this.req.end_time));
					this.advancedSearch = true;
				}
			}

			this.req.page = uni.getStorageSync('__paginationPage' + this.$route.path) || 1;
			this.req.rows = uni.getStorageSync('__paginationRows' + this.$route.path) || 10;

			this.loadData(() => {
				this.$nextTick(() => {
					this.$api.scrollToStorage('.el-table__body-wrapper', this.$route.path);
				});
			});
		},
		mounted() {
			this.$nextTick(() => {
				this.$api.scrollOnStorage('.el-table__body-wrapper', this.$route.path);
			});
		},
		updated() {
			if (!this.tblHeight) {
				this.tblHeight = this.$refs.tbl.tblHeight;
			}
		}
	};
</script>

<style>
	.ellipsis_2 {
		max-width: 100px;
	}
</style>
